<template>
  <el-dialog :visible="visible" :title="title" width="20%" @close="close">
    <div class="shelf-code">{{ shelfCode }}</div>
    <div class="code">
      <vue-qr ref="qrCode" :text="qrString" :size="240" :margin="10" />
      <div class="satge-name">{{ this.qrName }}</div>
    </div>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="downloadQR">下载</el-button>
    </div>
  </el-dialog>
</template>
<script>
import vueQr from 'vue-qr'
export default {
  name: 'QrCode',
  components: {
    vueQr
  },
  props: {
    title: {
      type: String,
      default: '二维码'
    },
    width: {
      type: String,
      default: '26%'
    }
  },
  data() {
    return {
      qrString: '',
      qrName: '',
      shelfCode: '',
      visible: false
    }
  },
  methods: {
    close() {
      this.visible = false
    },
    open(qrName, qrString, shelfCode) {
      this.qrName = qrName
      this.qrString = qrString
      this.shelfCode = shelfCode
      this.visible = true
    },
    // 下载事件处理函数
    downloadQR() {
      const a = document.createElement('a')
      a.download = '二维码'
      a.href = this.$refs.qrCode.$el.src
      a.click()
    }
  }
}
</script>
<style lang="scss" scoped>
.shelf-code {
  text-align: center;
  font-size: 25px;
  font-weight: bold;
}
.satge-name {
  text-align: center;
  font-size: 21px;
}
.code {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
</style>
